/*
  +++++++++++++++++++++++++++++++++++++++++++
  +  Description: 代码生成业务表
  +  Created by 王大宸 on 2023-09-23 21:25:55
  +  Created with IntelliJ IDEA.
  ++++++++++++++++++++++++++++++++++++++++++++
 */
const path = HTTP_BIZ_URI + "/dev/table/info";
layui.use(['table', 'form'], function () {
    let table = layui.table;
    let form = layui.form;

    /* 初始化表格 */
    $.table.init(table, options())

    table.on('tool(layFilter)', function (obj) {
        let data = obj.data;
        if ('update' === obj.event) {
            importTables(data.tableName);
        }
    })

    /* 查询按钮 */
    form.on('submit(searchDevTableInfo)', function (data) {
        reloadTableData(table);
        return false; // 阻止默认 form 跳转
    });

    /* 导入表 */
    form.on('submit(importDevTableInfo)', function (dataValue) {
        let checkStatus = table.checkStatus('layTable');
        let data = checkStatus.data;
        if (data.length === 0) {
            layer.alert("请选择需要的表", {
                time: 5000,
                btn: ['确定'],
                icon: 0
            });
            return false;
        }
        let tableNames = [];
        for (let i = 0; i < data.length; i++) {
            tableNames.push(data[i].tableName);
        }


        importTables(tableNames.join());

      //  return false; // 阻止默认 form 跳转
    })

});

/**
 * 数据表格查询条件(必须有,不然表格重载不了)
 */
function queryWhere() {
    return {
        tableName: $("#tableName").val()
    }
}

/**
 * 刷新表格数据
 * @param table
 */
function reloadTableData(table) {
    $.table.reloadData(table, options());
}

function options() {
    return {
        url: path + "/proto/list",
        where: queryWhere(),
        toolbar: false,
        defaultToolbar: false,
        cols: [[
            {type: 'checkbox'},
            {field: 'tableName', title: '表名称', align: "center"},
            {field: 'tableComment', title: '表描述', align: "center"},
            {fixed: 'right', title: '操作', toolbar: '#toolbarHandle', align: "center"},
        ]]
    }

}


function importTables(tableNames) {
    $.http.get({
        url: path + "/import/tables?tableNames=" + tableNames,
        callback: function (res) {
            if (200 === res.status) {

                parent.reloadTableData();
                $.msg.okMsg('导入成功');

                // layer.alert('导入成功', {title: "系统提示", icon: 1}, function () {
                //    // goBackPreviousPage();
                //     // location.href = HTTP_BIZ_URI + '/';
                //     $.model.close();
                // });
            } else {
                layer.alert(res.msg, {title: "系统提示", icon: 2});
            }
        }
    })
}

